package com.qjdchina.sku.util;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.util.StringUtils;

import com.qjdchina.common.web.consts.SessionKeyConsts;
import com.qjdchina.sku.consts.SessionKey;
import com.qjdchina.sku.model.vo.VerifyCodeVo;


/**
 * 
 * 类名:SessionUtils    
 * 功能:session 工具类     
 * 日期: 2016年1月31日 上午12:02:52    
 * @author wangzheng  
 * @version 1.0.0   
 * @since  JDK 1.7  
 * @see
 */
public final class SessionUtils {

    private SessionUtils() {

    }

    /**
     * 判断session是否存在
     * 
     * @param session
     * @return
     */
    public static boolean isSessionExist(HttpSession session) {
        if (session == null) {
            return false;
        }
        return true;
    }

    /**
     * 判断 session 值是否存在
     * 
     * @param value
     * @param session
     * @return
     */
    public static boolean isSessionValueExist(String value, HttpSession session) {
        if (isSessionExist(session)) {
            if (StringUtils.isEmpty(session.getAttribute(value))) {
                return false;
            }
            return true;
        }
        return false;
    }

    /**
     * 获取 session中的值
     * 
     * @param value
     * @param session
     * @return
     */
    public static Object getSessionValue(String value, HttpSession session) {
        if (isSessionValueExist(value, session)) {
            return session.getAttribute(value);
        }
        return null;
    }

    /**
     * 返回session中会员ID
     * 
     * @param session
     * @return
     */
    public static String getSessionMemberId(HttpSession session) {
        String memberId = null;
        if (isSessionValueExist(SessionKeyConsts.USER_ID, session)) {
            memberId = (String) session.getAttribute(SessionKeyConsts.USER_ID);
        }
        return memberId;
    }

    /**
     * 根据HttpServletRequest获取session中的会员ID
     * 
     * @param request
     * @return
     */
    public static String getSessionMemberId(HttpServletRequest request) {
        HttpSession session = request.getSession();
        String memberId = null;
        if (isSessionValueExist(SessionKeyConsts.USER_ID, session)) {
            memberId = (String) session.getAttribute(SessionKeyConsts.USER_ID);
        }
        return memberId;
    }


    /**
     * 返回session中OFS公司ID
     * 
     * @param session
     * @return
     */
    public static String getSessionOfsCompanyId(HttpSession session) {
        String ofsCompanyId = null;
        if (isSessionValueExist(SessionKey.OFS_COMPANY_ID, session)) {
            ofsCompanyId = (String) session.getAttribute(SessionKey.OFS_COMPANY_ID);
        }
        return ofsCompanyId;
    }

    /**
     * 根据HttpServletRequest获取session中的OFS公司ID
     * 
     * @param request
     * @return
     */
    public static String getSessionOfsCompanyId(HttpServletRequest request) {
        HttpSession session = request.getSession();
        String ofsCompanyId = null;
        if (isSessionValueExist(SessionKey.OFS_COMPANY_ID, session)) {
            ofsCompanyId = (String) session.getAttribute(SessionKey.OFS_COMPANY_ID);
        }
        return ofsCompanyId;
    }
    
    public static VerifyCodeVo getSessionOfsVerifyCode(HttpSession session) {
        VerifyCodeVo verifyCodeVo = null;
        if (isSessionValueExist(SessionKey.VERIFY_CODE_VO, session)) {
        	verifyCodeVo = (VerifyCodeVo)session.getAttribute(SessionKey.VERIFY_CODE_VO);
        }
        return verifyCodeVo;
    }
    
    /**
     * 获取后台登陆的帐号
     */
    public static String getSessionUserId(HttpSession session){
    	String userId = (String) session.getAttribute(SessionKeyConsts.CURRENT_ADMIN_USER);
    	return userId;
    }
    
    
}
